<?php

class PrivilegeController extends AdminhtmlController
{
	public $layout='application.modules.admin.views.layouts.external';
	
	/* this function must write final controller file!! */
	protected function getActionsKey()
	{
		return __CLASS__;
	}
	
	public function init()
	{
		parent::init();
		$this->controllerLable = '登录管理';
	}
	
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
				'foreColor'=>0x2040A0,
				'maxLength'=>5,
				'minLength'=>6,
				'padding'=>1,
				//'width'=>'120px',
				//'height'=>'20px',
				//'fixedVerifyCode'=>'hello',
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	
	public function getBreadcrumbs()
	{
		return $this->breadcrumbs=array(
			$this->getControllerLabel() =>array('index'),
			$this->getActionLabel(),
		);
	}
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$this->layout= 'application.modules.admin.views.layouts.column1';
		$this->setUnionTitle('欢迎页面');
		$this->render('index');
	}

	public function actionProfile()
	{
		$this->layout= 'application.modules.admin.views.layouts.column1';
		$this->setUnionTitle('我的帐户信息');
		$model=AdminhtmlAdmin::model()->findByAttributes(array('username'=>Yii::app()->admin->name));
		if(isset($_POST['AdminhtmlAdmin']))
		{
			if ( md5($_POST['AdminhtmlAdmin']['password_cr']) !=$model->getAttribute('password') ) {
				Yii::app()->admin->putErrorMsg('当前密码错误！');
				
			} elseif($_POST['AdminhtmlAdmin']['password']!= $_POST['AdminhtmlAdmin']['password_cf']) {
				Yii::app()->admin->putErrorMsg('两次密码输入不一致！');
				
			} else {
				$_POST['AdminhtmlAdmin']['password']= md5($_POST['AdminhtmlAdmin']['password']);
				$model->attributes=$_POST['AdminhtmlAdmin'];
				if($model->save()){
					Yii::app()->admin->putSuccessMsg('成功保存信息！');
			    }
			}
		}
		$model->setAttribute('password', '******');
		$this->render('profile',array( 'model'=>$model, ));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    $this->setUnionTitle('错误页面');
		if($error=Yii::app()->errorHandler->error) {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		if( !Yii::app()->admin->getIsGuest() ) {
			$this->redirect(array('index'));
		}
		
		$this->setUnionTitle('管理员登录');
		$model=new AdminhtmlLoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['AdminhtmlLoginForm'])) {
			/* @var $model CFormModel */
			$model->attributes=$_POST['AdminhtmlLoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login()) {
				if (isset($_GET['referrer'])) {
					$r= Yii::app()->createAbsoluteUrl(base64_decode($_GET['referrer']))  ;
				} else {
					$r= Yii::app()->admin->returnUrl;
				}
				$this->redirect($r);
			}
		}
		// display the login form
		$model->password= null;
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->admin->logout();
		//$this->redirect(Yii::app()->homeUrl);
		$this->render('logout');
	}
	
	public function actionNoPrivilege()
	{
		$message = '你无权进行此操作，页面将会在几秒钟后跳转。';
		if(isset($_POST['ajax']) ) {
			echo CJSON::encode(array('status'=>0, 'message'=>$message ));
		} else {
			$this->setUnionTitle('无权操作');
			$this->render('noPrivilege', array('message'=>$message, 'returnUrl'=>base64_decode($_GET['returnUrl']) ));
		}
	}
	
	protected function beforeAction($action)
	{
		/* @var CWebApplication */
	    if( !$this->isExceptAction() && Yii::app()->admin->getIsGuest() ){
			$this->redirect(array('privilege/login'));
		}
		if ( !$this->isExceptAction() && Yii::app()->admin->getState('area')!==Yii::app()->params['area']['adminhtml']  ) {
			$this->redirect(array('privilege/login'));
			
		} else {
		    return true;
		}
	}
	
}